package 多线程初阶.示例测试;

/**
 * WAITING,BLOCKED,TIMED_WAITING 状态的转换
 */
public class TestDemo11 {
    public static void main(String[] args) {
        final Object lock=new Object();
        Thread threadA=new Thread(()->{
            synchronized (lock){
                while (true){
                    try {
                        //Thread.sleep(1000);//sleep导致线程处于阻塞队列中,状态为TIME_WAITING
                        lock.wait();//导致线程状态改变为WAITING
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        },"threadA");
        threadA.start();
        Thread threadB=new Thread(()->{
           synchronized (lock){
               System.out.println("test");//处于BLOCKED状态,由于lock已经被threadA获取到了,这里再要获取就要等待线程threadA释放锁
           }
        },"threadB");
        threadB.start();
    }
}
